package org.grottarossa.ui.core.controls.searchuser;

import java.util.Collection;
import java.util.Hashtable;
import java.util.ResourceBundle;

import org.grottarossa.template.data.enums.EnumDataType;
import org.grottarossa.template.data.pojo.table.Cell;
import org.grottarossa.template.data.pojo.table.Record;
import org.grottarossa.template.data.pojo.table.Table;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.BasePanelView;
import org.grottarossa.ui.core.components.TMSListSelect;
import org.grottarossa.ui.core.container.TableContainer;

import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;


/***
 * Controllo di ricerca e selezione utente/ruolo
 * @author 
 *
 */
public class SearchUser extends BasePanelView<SearchUserPresenter> implements ClickListener
{
	private SearchUserForm m_searchForm;
	private Button m_btnFind;
	private TMSListSelect m_results;
	private Button m_btnCancel;
	private Button m_btnOk;

	public SearchUser(ApplicationSession appSession, ResourceBundle localizer)
	{
		super(appSession, localizer);
		initializeComponents();
	}

	@Override
	protected void initializeComponents()
	{
		VerticalLayout vLayout = new VerticalLayout();
		vLayout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		vLayout.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		vLayout.setSpacing(true);

		m_searchForm = new SearchUserForm(m_appSession, m_localizer);
		m_searchForm.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		vLayout.addComponent(m_searchForm);

		m_btnFind = new Button(m_localizer.getString("form.buttons.find"));
		m_btnFind.addListener(this);
		vLayout.addComponent(m_btnFind);

		m_results = new TMSListSelect(m_localizer.getString("form.results.caption"), "");
		m_results.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		m_results.setHeight("200px");
		m_results.setMultiSelect(false);
		vLayout.addComponent(m_results);

		m_btnOk = new Button(m_localizer.getString("form.buttons.ok"));
		m_btnOk.addListener(this);

		m_btnCancel = new Button(m_localizer.getString("form.buttons.cancel"));
		m_btnCancel.addListener(this);

		HorizontalLayout buttonsBar = new HorizontalLayout();
		buttonsBar.addComponent(m_btnOk);
		buttonsBar.addComponent(m_btnCancel);
		vLayout.addComponent(buttonsBar);

		setLayout(vLayout);
	}

	public void buttonClick(ClickEvent event)
	{
		try
		{
			if(event.getButton() == m_btnFind)
				m_presenter.search();
			else if(event.getButton() == m_btnOk)
				m_presenter.ok();
			else if(event.getButton() == m_btnCancel)
				m_presenter.cancel();
		}
		catch(Exception ex)
		{
			showError(ex);
		}
	}

	/***
	 * Valorizza la form di ricerca
	 * @param m_filter
	 */
	public void setSearchFilter(SearchUserData m_filter)
	{
		m_searchForm.setItemDataSource(new BeanItem<SearchUserData>(m_filter));
	}

	/***
	 * Popola la lista dei risultati
	 * @param searchResults
	 */
	public void showResults(Hashtable<String, String> searchResults)
	{
		Table table = new Table();
		table.getHeader().add(new Cell("key", EnumDataType.String, "key"));
		table.getHeader().add(new Cell("value", EnumDataType.String, "value"));

		for(String loginId : searchResults.keySet())
		{
			Record record = new Record ();
			record.add(new Cell("key", EnumDataType.String, searchResults.get(loginId)));
			record.add(new Cell("value", EnumDataType.String, loginId));
			table.getRecords().add(record);
		}

		TableContainer tc = new TableContainer("value");
		tc.setDataTable(table);

		m_results.setItemCaptionPropertyId("key");
		m_results.setContainerDataSource(tc);
	}

	/***
	 * Ritorna l'utente/ruolo selezionato
	 * @return
	 */
	public String getSelectedUser()
	{
		if(m_results.getValue() == null)
			return null;

		return (String)m_results.getValue();
	}

	/***
	 * Aggiunge un listener
	 * @param listener
	 */
	public void addListener(ISearchUserListener listener)
	{
		m_presenter.addListener(listener);
	}

}
